package main

import (
	"fmt"
	"html/template"
	"net/http"
)

//在模板引擎中使用，首字母需要大写
type User struct {
	Name   string
	Gender string
	Age    int
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	//1.定义模板
	//2.解析模板
	t, err := template.ParseFiles("./hello.tmpl")
	if err != nil {
		fmt.Printf("Parse templates failed,err:%v \n", err)
		return
	}
	//3.渲染模板
	u1 := User{ //u1.Name
		"小王子",
		"男",
		18,
	}
	m1 := map[string]interface{}{
		"name":   "wer",
		"gender": "nv",
		"age":    20,
	}
	hobbyList:=[]string{
		"篮球",
		"足球",
		"乒乓球",
	}
	//t.Execute(w, u1)
	//t.Execute(w, m1)
	t.Execute(w, map[string]interface{}{
		"u1": u1,
		"m1": m1,
		"hobby":hobbyList,
	})
}

func main() {
	http.HandleFunc("/", sayHello)
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		fmt.Println("HTTP server start failed.err:%v", err)
		return
	}
}
